package com.atguigu.ssm.controller;

import com.atguigu.ssm.pojo.Employee;
import com.atguigu.ssm.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 查询所有员工信息-->/employee-->get
 * 根据id 查询某个员工的信息-->/employee/1---->get
 * 跳转到添加页面-->to/add-->get
 * 添加员工信息--->/employee--->post
 * 修改员工信息--->/employee--->put
 * 删除员工信息--->/employee/1--->delete
 */
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/employee/{pageNum}",method = RequestMethod.GET)
    public String getAllEmployee(Model model, @PathVariable("pageNum") Integer pageNum){
        //查询所有的员工信息
        PageInfo<Employee> page = employeeService.getEmployeePage(pageNum);
        model.addAttribute("page",page);
        return "employee_list";
    }
    @RequestMapping(value = "/employee",method = RequestMethod.GET)
    public String getAllEmployeeList(Model model){
        List<Employee> list = employeeService.getAllEmployee();
        model.addAttribute("list",list);
        return "employee_list";
    }
    @RequestMapping(value = "/to/add",method = RequestMethod.GET)
    public String toAdd(){
        return "employee_add";
    }
    @RequestMapping(value = "/employee",method = RequestMethod.POST)
    public String doEmployee(Employee employee){
        System.out.println("employee:"+employee);
        employeeService.save(employee);
        return "redirect:/employee/1";
    }
    @RequestMapping(value = "/employee/{empId}",method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("empId") Integer empId){
        System.out.println("empId:"+empId);
        employeeService.deleteEmployee(empId);
        return "redirect:/employee/1";
    }
    @RequestMapping(value = "/update/{empId}",method = RequestMethod.GET)
    public String getEmployeeById(@PathVariable("empId") Integer empId,Model model){
        System.out.println("empId:"+empId);
        Employee employee = employeeService.getEmployeeById(empId);
        System.out.println("employee:"+employee);
        model.addAttribute("employee",employee);
        return "employee_update";
    }
    //处理修改员工信息
    @RequestMapping(value = "/employee",method = RequestMethod.PUT)
    public String updateEmployee(Employee employee){
        System.out.println("employee:"+employee);
        employeeService.updateEmp(employee);
        return "redirect:/employee/1";
    }
}
